#include "cadlight.h"

static QStringList LIST_LIGHT_OPEN = (QStringList() << "开" << "打开" );
static QStringList LIST_LIGHT_CLOSE = (QStringList() << "关" << "关闭" << "灭");
static QStringList LIST_LIGHT_DIM = (QStringList() << "亮度");

static QStringList LIST_LIGHT_NAME = (QStringList() << "阅读灯" << "排气扇" << "吊灯" << "灯带" << "筒灯"
                                      << "玄关灯" << "台灯" << "壁灯"
                                      << "夜灯" << "地脚灯" << "射灯"
                                      << "吸顶灯" << "落地灯" << "阳台灯"
                                      << "走廊灯" << "小灯" << "大灯"
                                      << "日光灯" << "电视灯" << "投影灯");


static QStringList LIST_EQUAL = (QStringList() << "=" << "设为" << "设置" );
static QStringList LIST_ADD = (QStringList() << "+" << "加" << "高" << "升" << "上调" << "增加");
static QStringList LIST_SUB = (QStringList() << "-" << "减" << "低" << "降" << "下调" << "减小");

CADLight::CADLight(CAudioControlDevice *parent)
    :CAudioControlDevice(parent)
{


}

bool CADLight::isActionWithOnlyArea(QString word, QString &name)
{
    foreach (QString l, DEVICE_OPERATION::LIST_AREA_NAME) {
        if(word.contains(l)){
            QString s = word.remove(l);
            name = l;
            {
                if(s=="开" || s=="打开")
                    return true;

                if(s=="关" || s=="关闭")
                    return true;
            }
        }
    }

    return false;
}

DEVICE_OPERATION * CADLight:: parseWord(QString word)
{
    QString op,name,value,calc,area;
    bool action_all = false;
    qDebug() << "CADLight: parseWord " << word ;

    bool area_action = false;

    if(isActionWithOnlyArea(word,area))
    {
        name = area;        
        area_action = true;

        foreach (QString s, LIST_LIGHT_OPEN) {
            if(word.contains(s))
            {
                op = DEVICE_ACTION_SWITCH;
                value = DEVICE_STATUS_ON;
                break;
            }
        }

        if(op.isEmpty())
        {
            foreach (QString s, LIST_LIGHT_CLOSE) {
                if(word.contains(s))
                {
                    op = DEVICE_ACTION_SWITCH;
                    value = DEVICE_STATUS_OFF;
                    break;
                }
            }
        }

        if(op.isEmpty())
        {
            foreach (QString s, LIST_LIGHT_DIM) {
                if(word.contains(s))
                {
                    op = DEVICE_ACTION_DIM;
                    value = match_dim_value(word);
                    calc = match_add_sub(word);

                    break;
                }
            }
        }

    }
    else
    {
        //查找项目中的匹配设备，因为配置很多什么吧台射灯这样的随意名称
        foreach (DEVICE_OPERATION *s, _device_list) {
            if(word.contains(s->_name))
            {
                name = s->_name;
                break;
            }
        }
        //使用通用名称匹配
        if(name.isEmpty())
        {
            foreach (QString s, LIST_LIGHT_NAME) {
                if(word.contains(s))
                {
                    name = s;
                    action_all = true;
                    break;
                }
            }
        }

        foreach (QString s, LIST_LIGHT_OPEN) {
            if(word.contains(s))
            {
                op = DEVICE_ACTION_SWITCH;
                value = DEVICE_STATUS_ON;
                break;
            }
        }

        if(op.isEmpty())
        {
            foreach (QString s, LIST_LIGHT_CLOSE) {
                if(word.contains(s))
                {
                    op = DEVICE_ACTION_SWITCH;
                    value = DEVICE_STATUS_OFF;
                    break;
                }
            }
        }

        if(op.isEmpty())
        {
            foreach (QString s, LIST_LIGHT_DIM) {
                if(word.contains(s))
                {
                    op = DEVICE_ACTION_DIM;
                    value = match_dim_value(word);
                    calc = match_add_sub(word);

                    break;
                }
            }
        }

        if(op.isEmpty())
        {
            value = match_dim_value(word);
            if(!value.isEmpty())
            {
                calc = match_add_sub(word);
                op = DEVICE_ACTION_DIM;
            }
        }
    }

    if(name.isEmpty() || op.isEmpty() || value.isEmpty())
    {
        //qDebug()<< "Audio: could not found device " << word;
        return nullptr;
    }

    qDebug()<< "Audio: CADLight found device " << name << " = " << value;

    DEVICE_OPERATION *device_op = new DEVICE_OPERATION();
    device_op->_audio_name = name;
    device_op->_audio_operation = op;
    device_op->_audio_value = value;
    device_op->_audio_calc = calc;
    device_op->_audio_action_dev_all = action_all;

    return device_op;
}

QString CADLight::match_dim_value(QString word)
{

    QRegExp rx("[0-9]{1,3}%");
    QString value;
    if(rx.indexIn(word) != -1) {
         value = rx.cap(0);
    }

    return value;
}

QString CADLight::match_add_sub(QString word)
{
    foreach (QString s, LIST_ADD) {
        if(word.contains(s))
        {
            return LIST_ADD.at(0);
        }
    }

    foreach (QString s, LIST_SUB) {
        if(word.contains(s))
        {
            return LIST_SUB.at(0);
        }
    }

    return LIST_EQUAL.at(0);
}

void CADLight::addDevice(DEVICE_OPERATION *d)
{
    _device_list.append(d);
}

void CADLight::clearDevice()
{
    foreach (DEVICE_OPERATION *d, _device_list) {
        d->deleteLater();
    }

    _device_list.clear();
}

bool CADLight::actionAudionControl(DEVICE_OPERATION *devOp)
{
    devOp->_match = false;

    foreach (DEVICE_OPERATION *d, _device_list) {
        if(devOp->_audio_action_dev_all && d->_name.contains(devOp->_audio_name))
        {
            devOp->_id_all << d->_id;
        }
        else if(devOp->_audio_area_action && d->_name.contains(devOp->_audio_name) &&  d->_name.contains("灯"))
        {
            devOp->_id_all << d->_id;
        }
        else if(d->_name.contains(devOp->_audio_name) )
        {
            devOp->_id = d->_id;
            devOp->_name = d->_name;
            devOp->_match = true;

            return true;
        }

    }

    return false;
}
